package com.uzai.console.service.sop.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.BaseDto;
import com.uzai.console.dto.sop.sopprirewardrule.SopPriRewardRuleDto;
import com.uzai.console.dto.sop.sopprirewardrule.SopPriRewardRuleIdDto;
import com.uzai.console.dto.sop.sopprirewardrule.SopPriRewardRuleQuery;
import com.uzai.console.dto.sop.sopprirewardrule.SopPriRewardRuleSendParaDto;
import com.uzai.console.dto.sop.sopprirewardrulestrategy.SopPriRewardRuleStrategyIdDto;
import com.uzai.console.entity.MarketingTag;
import com.uzai.console.entity.SopPriRewardRule;
import com.uzai.console.entity.SopPriRewardRuleStrategy;
import com.uzai.console.jsonvo.sop.SopPriRewardRuleSendPara;
import com.uzai.console.mapper.MarketingTagMapper;
import com.uzai.console.mapper.SopPriRewardRuleMapper;
import com.uzai.console.mapper.SopPriRewardRuleStrategyMapper;
import com.uzai.console.service.sop.SopPriRewardRuleService;
import com.uzai.console.vo.sop.sopprirewardrule.SopPriRewardRuleSendParaVo;
import com.uzai.console.vo.sop.sopprirewardrule.SopPriRewardRuleVo;
import com.uzai.console.vo.user.SysTagShow;
import com.uzai.console.vo.user.SysTagVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * SOP任务
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
@Slf4j
public class SopPriRewardRuleServiceImpl implements SopPriRewardRuleService {

    @Autowired
    private SopPriRewardRuleMapper sopPriRewardRuleMapper;
    @Autowired
    private MarketingTagMapper marketingTagMapper;
    @Autowired
    private SopPriRewardRuleStrategyMapper sopPriRewardRuleStrategyMapper;


    /**
     * 查询进粉渠道信息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return SopPriRewardRuleVo
     */
    public Page<SopPriRewardRuleVo> findByPage(SopPriRewardRuleQuery sopPriRewardRuleQuery){

        //新建返回到页面的LIST对象
        List<SopPriRewardRuleVo> sopPriRewardRuleVoList = new ArrayList<>();

        //执行分页查询对象
        Page<SopPriRewardRule> page = new Page<>();
        sopPriRewardRuleQuery.convert(page);
        List<SopPriRewardRule> sopPriRewardRuleList = sopPriRewardRuleMapper.findByList(page, sopPriRewardRuleQuery);
        if(sopPriRewardRuleList != null){
            for (SopPriRewardRule sopPriRewardRule : sopPriRewardRuleList){
                SopPriRewardRuleVo sopPriRewardRuleVo = new SopPriRewardRuleVo();
                BeanUtils.copyProperties(sopPriRewardRule, sopPriRewardRuleVo);

                //会员标签
                if(StringUtils.isNotBlank(sopPriRewardRule.getSysTags())){
                    try {
                        List<SysTagVo> sysTagVoList =  JSONObject.parseArray(sopPriRewardRule.getSysTags(), SysTagVo.class);
                        if(sysTagVoList != null && sysTagVoList.size() > 0){
                            List<SysTagShow> sysTagShowList = new ArrayList<>();
                            List<Long> sysTagsArr = new ArrayList<>();
                            for (SysTagVo sysTagVo : sysTagVoList) {
                                sysTagsArr.add(sysTagVo.getId());
                                SysTagShow sysTagShow = new SysTagShow();
                                BeanUtils.copyProperties(sysTagVo, sysTagShow);
                                //查询标签名称
                                MarketingTag marketingTag = marketingTagMapper.selectById(sysTagVo.getId(), sopPriRewardRule.getMerId());
                                if(marketingTag != null){
                                    sysTagShow.setName(marketingTag.getName());
                                }
                                sysTagShowList.add(sysTagShow);
                            }
                            sopPriRewardRuleVo.setSysTagsArr(sysTagsArr);
                            sopPriRewardRuleVo.setSysTagVoArr(sysTagShowList);
                        }
                    }catch (Exception e){ //如果不是JSON格式

                    }
                }

                //策略名称
                if(sopPriRewardRule.getStrategyId() != null){
                    SopPriRewardRuleStrategy sopPriRewardRuleStrategy = sopPriRewardRuleStrategyMapper.selectById(sopPriRewardRule.getStrategyId(), sopPriRewardRule.getMerId());
                    if(sopPriRewardRuleStrategy != null){
                        sopPriRewardRuleVo.setStrategyName(sopPriRewardRuleStrategy.getName());
                    }
                }

                sopPriRewardRuleVoList.add(sopPriRewardRuleVo);
            }
        }
        //返回到页面分页对象
        Page<SopPriRewardRuleVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(sopPriRewardRuleVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询待维护会员
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param sopPriRewardRuleIdDto
     * @return null
     */
    public SopPriRewardRuleVo findById(SopPriRewardRuleIdDto sopPriRewardRuleIdDto){
        SopPriRewardRuleVo sopPriRewardRuleVo = new SopPriRewardRuleVo();
        if(sopPriRewardRuleIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }
        SopPriRewardRule sopPriRewardRule = sopPriRewardRuleMapper.selectById(sopPriRewardRuleIdDto.getId(), sopPriRewardRuleIdDto.getMerId());
        if(sopPriRewardRule != null){
            BeanUtils.copyProperties(sopPriRewardRule, sopPriRewardRuleVo);

            if(StringUtils.isNotBlank(sopPriRewardRule.getSysTags())){
                try {
                    List<SysTagVo> sysTagVoList =  JSONObject.parseArray(sopPriRewardRule.getSysTags(), SysTagVo.class);
                    if(sysTagVoList != null && sysTagVoList.size() > 0){
                        List<SysTagShow> sysTagShowList = new ArrayList<>();
                        List<Long> sysTagsArr = new ArrayList<>();
                        for (SysTagVo sysTagVo : sysTagVoList) {
                            sysTagsArr.add(sysTagVo.getId());
                            SysTagShow sysTagShow = new SysTagShow();
                            BeanUtils.copyProperties(sysTagVo, sysTagShow);
                            //查询标签名称
                            MarketingTag marketingTag = marketingTagMapper.selectById(sysTagVo.getId(), sopPriRewardRule.getMerId());
                            if(marketingTag != null){
                                sysTagShow.setName(marketingTag.getName());
                            }
                            sysTagShowList.add(sysTagShow);
                        }
                        sopPriRewardRuleVo.setSysTagsArr(sysTagsArr);
                        sopPriRewardRuleVo.setSysTagVoArr(sysTagShowList);
                    }
                }catch (Exception e){ //如果不是JSON格式

                }
            }

            //策略名称
            if(sopPriRewardRule.getStrategyId() != null){
                SopPriRewardRuleStrategy sopPriRewardRuleStrategy = sopPriRewardRuleStrategyMapper.selectById(sopPriRewardRule.getStrategyId(), sopPriRewardRule.getMerId());
                if(sopPriRewardRuleStrategy != null){
                    sopPriRewardRuleVo.setStrategyName(sopPriRewardRuleStrategy.getName());
                }
            }
        }
        return sopPriRewardRuleVo;
    }

    /**
     * 删除进粉渠道信息
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param sopPriRewardRuleIdDto
     * @return null
     */
    public void deleteSopPriRewardRule(SopPriRewardRuleIdDto sopPriRewardRuleIdDto){
        if(sopPriRewardRuleIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        SopPriRewardRule sopPriRewardRule = sopPriRewardRuleMapper.selectById(sopPriRewardRuleIdDto.getId(), sopPriRewardRuleIdDto.getMerId());

        if(sopPriRewardRule == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        //删除进粉渠道
        sopPriRewardRuleMapper.deleteById(sopPriRewardRuleIdDto.getId(), sopPriRewardRuleIdDto.getMerId());

    }

    /**
     * 修改进粉渠道信息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public void updateSopPriRewardRule(SopPriRewardRuleDto sopPriRewardRuleDto){

        if(sopPriRewardRuleDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        SopPriRewardRule sopPriRewardRule = sopPriRewardRuleMapper.selectById(sopPriRewardRuleDto.getId(), sopPriRewardRuleDto.getMerId());

        if(sopPriRewardRule == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请进粉渠道不存在");
        }

        int now =  DateUtil.getNowTime();

        sopPriRewardRule.setStrategyId(sopPriRewardRuleDto.getStrategyId());
        sopPriRewardRule.setSysTags(JSONObject.toJSONString(sopPriRewardRuleDto.getSysTagList()));
        sopPriRewardRule.setMsg(sopPriRewardRuleDto.getMsg());
        sopPriRewardRule.setUpdateTime(now);

        sopPriRewardRuleMapper.updateById(sopPriRewardRule);

    }

    /**
     * 修改Sop定向规则发送参数
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param sopPriRewardRuleSendParaDto
     * @return null
     */
    public void updateSendPara(SopPriRewardRuleSendParaDto sopPriRewardRuleSendParaDto){

        SopPriRewardRuleSendPara sopPriRewardRuleSendPara = new SopPriRewardRuleSendPara();
        sopPriRewardRuleSendPara.setTimeInterval(sopPriRewardRuleSendParaDto.getTimeInterval());
        sopPriRewardRuleSendPara.setBusyTimeInterval(sopPriRewardRuleSendParaDto.getBusyTimeInterval());
        sopPriRewardRuleSendPara.setLimitTimeStart(sopPriRewardRuleSendParaDto.getLimitTimeStart());
        sopPriRewardRuleSendPara.setLimitTimeEnd(sopPriRewardRuleSendParaDto.getLimitTimeEnd());
        String sendPara = JSONObject.toJSONString(sopPriRewardRuleSendPara);

        sopPriRewardRuleMapper.updateSendParaBatch(sendPara, sopPriRewardRuleSendParaDto.getMerId());

    }

    /**
     * 查询Sop定向规则发送参数
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param baseDto
     * @return null
     */
    public SopPriRewardRuleSendParaVo findSendPara(BaseDto baseDto){
        SopPriRewardRuleSendParaVo sopPriRewardRuleSendParaVo = new SopPriRewardRuleSendParaVo();

        //获取第一个
        SopPriRewardRuleQuery sopPriRewardRuleQuery = new SopPriRewardRuleQuery();
        sopPriRewardRuleQuery.setMerId(baseDto.getMerId());
        List<SopPriRewardRule> sopPriRewardRuleList = sopPriRewardRuleMapper.findByList(sopPriRewardRuleQuery);
        if(sopPriRewardRuleList != null && sopPriRewardRuleList.size() > 0){
            SopPriRewardRule sopPriRewardRule = sopPriRewardRuleList.get(0);
            if(StringUtils.isNotBlank(sopPriRewardRule.getSendPara())){
                sopPriRewardRuleSendParaVo = JSONObject.parseObject(sopPriRewardRule.getSendPara(), SopPriRewardRuleSendParaVo.class);
            }
        }

        return sopPriRewardRuleSendParaVo;
    }


}
